package com.wallet.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSON;
import com.wallet.entity.form.ArticleForm;
import com.wallet.entity.form.RoutesForm;

public class HttpClientUtil {
	
	public static String httpclientGet(String urlStr) throws Exception{
//		System.out.println(urlStr);  
		  
        // 创建HttpClient对象  
        HttpClient client = HttpClients.createDefault();  
  
        // 创建GET请求（在构造器中传入URL字符串即可）  
        HttpGet get = new HttpGet(urlStr);  
  
        // 调用HttpClient对象的execute方法获得响应  
        HttpResponse response = client.execute(get);  
  
        // 调用HttpResponse对象的getEntity方法得到响应实体  
        HttpEntity httpEntity = response.getEntity();
        
        // 使用EntityUtils工具类得到响应的字符串表示  
        return EntityUtils.toString(httpEntity, "utf-8");
	}
	

	//获取key和value
	public static void entrySetIteratorGetKeyAndValue(Map<String, Object> map){
	    Iterator<Entry<String, Object>> iterator = map.entrySet().iterator();
	    while (iterator.hasNext()) {
	        String key = iterator.next().getKey();
	        String value = map.get(key).toString();
	        System.out.println(key);
	        System.out.println(value);
	    }
	}
	
	public static String get(String url){
		String result = "";
		HttpGet get = new HttpGet(url);
		try{
			CloseableHttpClient httpClient = HttpClients.createDefault();
			
			HttpResponse response = httpClient.execute(get);
			result = getHttpEntityContent(response);
			
			if(response.getStatusLine().getStatusCode()!=HttpStatus.SC_OK){
				result = "服务器异常";
			}
		} catch (Exception e){
			System.out.println("请求异常");
			throw new RuntimeException(e);
		} finally{
			get.abort();
		}
		return result;
	}
	
	public static String getHttpEntityContent(HttpResponse response) throws UnsupportedOperationException, IOException{
		String result = "";
		HttpEntity entity = response.getEntity();
		if(entity != null){
			InputStream in = entity.getContent();
			BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
			StringBuilder strber= new StringBuilder();
			String line = null;
			while((line = br.readLine())!=null){
				strber.append(line+'\n');
			}
			br.close();
			in.close();
			result = strber.toString();
		}
		
		return result;
		
	}
	
	public static List<RoutesForm> showChildren(List<RoutesForm> listEnd,RoutesForm routesForm,Integer treeline){
		List<RoutesForm>list=routesForm.getChildren();
		routesForm.setChildren(null);
		listEnd.add(routesForm);
		if(list!=null) {
			for (int i = 0; i < list.size(); i++) {
				showChildren(listEnd,list.get(i), treeline+1);
			}
		}
		return listEnd;
	}
	
	public static void main(String[] args) {
		try {
			String result=get("http://localhost:9527/dev-api/vue-element-admin/routes");
			Map<String, Object> map=(Map<String,Object>)JSON.parse(result);
			String data=map.get("data").toString();
			List<RoutesForm> routesForms=JSON.parseArray(data, RoutesForm.class);
			List<RoutesForm> routesEnd=new ArrayList<RoutesForm>();
			for (int i = 0; i < routesForms.size(); i++) {
				showChildren(routesEnd,routesForms.get(i), 1);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	private static void getArticle() {
		String result=get("http://localhost:9528/dev-api/vue-element-admin/article/list?page=1&limit=2");
		Map<String,Object> maps = (Map<String,Object>)JSON.parse(result);  
		Map<String,Object> data=(Map<String,Object>)maps.get("data");
		String items=data.get("items").toString();
		List<ArticleForm> articleEnd=JSON.parseArray(items, ArticleForm.class);
		System.out.println(articleEnd.toString());
	}
	

}
